package com.sheepone.util.number;

import java.util.Random;

/**
 * @author Miss.杨
 * @description
 * @since 2023-12-14 16:49
 */
public class NumberUtil {
    private NumberUtil() {
    }


    /**
     * 将一个Number对象转换为double类型
     *
     * @param number
     * @return
     */
    public static double numberToDouble(Object number) {
        if (number instanceof Number n) {
            return n.doubleValue();
        } else if (number == null) {
            return 0;
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

    /**
     * 将一个Number对象转换为int类型
     *
     * @param number
     * @return
     */
    public static int numberToInt(Object number) {
        if (number instanceof Number n) {
            return n.intValue();
        } else if (number == null) {
            return 0;
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

    /**
     * 将一个Number对象转换为long类型
     *
     * @param number
     * @return
     */
    public static long numberToLong(Object number) {
        if (number instanceof Number n) {
            return n.longValue();
        } else if (number == null) {
            return 0;
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

    /**
     * 将一个Number对象转换为float类型
     *
     * @param number
     * @return
     */
    public static float numberToFloat(Object number) {
        if (number instanceof Number n) {
            return n.floatValue();
        } else if (number == null) {
            return 0;
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

    /**
     * 将一个Number对象转换为short类型
     *
     * @param number
     * @return
     */
    public static short numberToShort(Object number) {
        if (number instanceof Number n) {
            return n.shortValue();
        } else if (number == null) {
            return 0;
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

    /**
     * 将一个Number对象转换为byte类型
     *
     * @param number
     * @return
     */
    public static byte numberToByte(Object number) {
        if (number instanceof Number n) {
            return n.byteValue();
        } else if (number == null) {
            return 0;
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

    /**
     * 将一个Number对象转换为指定的Number子类型类型
     *
     * @param number
     * @param type
     * @return
     */
    public static Object convertNumber(Object number, Class<? extends Number> type) {
        if (number instanceof Number n) {
            if (type == int.class || type == Integer.class) {
                return n.intValue();
            } else if (type == long.class || type == Long.class) {
                return n.longValue();
            } else if (type == float.class || type == Float.class) {
                return n.floatValue();
            } else if (type == double.class || type == Double.class) {
                return  n.doubleValue();
            } else if (type == short.class || type == Short.class) {
                return  n.shortValue();
            } else if (type == byte.class || type == Byte.class) {
                return  n.byteValue();
            }
        } else if (number == null) {
            return 0;
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

    public static Object newNumberArrayByType(int size, Class<? extends Number> type) {
        if (type == int.class || type == Integer.class) {
            if (type == Integer.class) {
                return new Integer[size];
            } else {
                return new int[size];
            }
        } else if (type == long.class || type == Long.class) {
            if (type == Long.class) {
                return new Long[size];
            } else {
                return new long[size];
            }
        } else if (type == float.class || type == Float.class) {
            if (type == Float.class) {
                return new Float[size];
            } else {
                return new float[size];
            }
        } else if (type == double.class || type == Double.class) {
            if (type == Double.class) {
                return new Double[size];
            } else {
                return new double[size];
            }
        } else if (type == short.class || type == Short.class) {
            if (type == Short.class) {
                return new Short[size];
            } else {
                return new short[size];
            }
        } else if (type == byte.class || type == Byte.class) {
            if (type == Byte.class) {
                return new Byte[size];
            } else {
                return new byte[size];
            }
        }
        throw new IllegalArgumentException("The provided object is not a Number or its subclass");
    }

}
